package gl.android.biyiqing.fragment;

import gl.App;
import gl.android.biyiqing.PassWordChangeActivity;
import gl.android.biyiqing.R;
import gl.android.jxzs.HtmlActivity;
import gl.common.AjaxCallBackProxy;
import gl.common.BaseControl;
import gl.common.UserManger;
import gl.uitls.AndroidUtil;
import gl.uitls.TextUtil;
import gl.uitls.Tip;

import org.json.JSONObject;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 消息盒子
 * 
 * @author geliang
 * 
 */
public class SettingFragment extends BaseListFragmet implements OnClickListener {

	private static final int LENGTH_PHONENUMBER = 11;
	private void bindViews() {
		findViewById(R.id.exit).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				AndroidUtil.saveToSharePreferences("isAutoLogin", "0", "false");
				App.getApp().exit();

			}
		});
		findViewById(R.id.rl_change_pw).setOnClickListener(this);
		findViewById(R.id.rl_about).setOnClickListener(this);
		findViewById(R.id.rl_help).setOnClickListener(this);
		findViewById(R.id.rl_feedback).setOnClickListener(this);
		findViewById(R.id.rl_tj).setOnClickListener(this);
	}

	public SettingFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.setting, container, false);
		bindViews();
		intRecommand();
		return rootView;
	}

	private String recommand="";
	private void intRecommand() {
		
		String url = "http://116.211.116.168:8080/by_webservice/GetUserRecomment?userid=%s"
				;
		BaseControl.get(url, new AjaxCallBackProxy() {

			@Override
			public void onSuccess(String t) {
				super.onSuccess(t);
				try {
					if ("1".equals(new JSONObject(t)
							.getString("msg"))) {
						final TextView findViewById = (TextView) findViewById(R.id.tv_tj);
						 recommand = new JSONObject(t).optString("recommend");
						findViewById.setText("推荐人:"+recommand);
						setNotAllowRecommond();
						return;
					}
				} catch (Exception e) {
					e.printStackTrace();
					onFailure(e, 0, e.getMessage());
				}
			}

			@Override
			public void onFailure(Throwable t,
					int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
			}
		}, new Object[]{UserManger.getInstance().userid});
		
	}

	@Override
	public void onClick(View v) {

		if (v.getId() == R.id.rl_change_pw) {

			startActivity(new Intent(getActivity(),
					PassWordChangeActivity.class));
		}
		else if (v.getId() == R.id.rl_about) {
			String url = "http://116.211.116.168:8080/newp/src/aboutby.html";
			startWebActivity(url);
		}
		else if (v.getId() == R.id.rl_help) {
			String url = "http://116.211.116.168:8080/newp/src/help.html";
			startWebActivity(url);
		}
		else if (v.getId() == R.id.rl_feedback) {
			String url = "http://116.211.116.168:8080/newp/src/feedback.html";
			startWebActivity(url);
		}
		else if (v.getId() == R.id.rl_tj) {
			final TextView findViewById = (TextView) findViewById(R.id.tv_tj);
			genRecommandNumberInputDialog("请输入推荐人的手机号码", "用户推荐", findViewById);
		}

	}

	public  void genRecommandNumberInputDialog(final String message,
			final String title, final TextView regist_email2) {
		final EditText et = new EditText(regist_email2.getContext());
		et.setInputType(InputType.TYPE_CLASS_NUMBER);
		if (!TextUtils.isEmpty(recommand)) {
			et.setText(recommand);
		}
		// 设置最大输入字符为3
		et.setFilters(new InputFilter[] { new InputFilter.LengthFilter(11) });
		AndroidUtil.genCustomViewDialog(regist_email2.getContext(), et,
				message, title, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						try {
							if (!TextUtils.isEmpty(et.getText())/*&&TextUtil.isMobileNO(et.getText().toString()*/&&et.getText().toString().length()==LENGTH_PHONENUMBER) {
								String url = "http://116.211.116.168:8080/by_webservice/SetUserRecomment?userid=%s&phone=%s";
								BaseControl.get(url, new AjaxCallBackProxy() {
									@Override
									public void onSuccess(String t) {
										super.onSuccess(t);
										try {
											if ("0".equals(new JSONObject(t)
													.getString("msg"))) {
												Tip.show("此号码已推荐");
												return;
											}
											if ("1".equals(new JSONObject(t)
													.getString("msg"))) {
												Tip.show("推荐失败");
												return;
											}
											Tip.show("成功");
											((TextView)findViewById(R.id.tv_tj)).setText("推荐人:"+et.getText().toString());
											setNotAllowRecommond();
										} catch (Exception e) {
											e.printStackTrace();
											onFailure(e, 0, e.getMessage());
										}
									}

									@Override
									public void onFailure(Throwable t,
											int errorNo, String strMsg) {
										super.onFailure(t, errorNo, strMsg);
										Tip.show("推荐失败");
									}
								}, new Object[]{UserManger.getInstance().userid,et.getText().toString()});
							}else{
								if (!TextUtils.isEmpty(et.getText())||et.getText().toString().length()!=LENGTH_PHONENUMBER) {
									Tip.show("请输入正确的手机号");
								}
							}
						} catch (Exception e) {
							e.printStackTrace();
						}

					}
				});
	}

	public void startWebActivity(String url) {
		final Intent intent = new Intent(getActivity(), HtmlActivity.class);
		intent.putExtra(HtmlActivity.KEY_URL, url);
		startActivity(intent);
	}

	public void setNotAllowRecommond() {
		findViewById(R.id.rl_tj).setEnabled(false);
		findViewById(R.id.iv_tj).setEnabled(false);
		findViewById(R.id.tv_tj).setEnabled(false);
	}
}